<?php

namespace App\Http\Controllers\Api;

use App\ApplicationRecord;
use App\Basic;
use App\Follow;
use App\Http\Requests\IsConsent;
use App\Http\Requests\PositionApply;
use App\Http\Requests\ReleasePosition;
use App\Invitationit;
use App\Position;
use App\User;
use App\UserPersonnel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

//面试
class InterviewController extends Controller
{
    /**
     * @param ReleasePosition $request
     * @param Position $position
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 发布职位
     */
    public function releasePosition(ReleasePosition $request, Position $position)
    {
        if (!\App\CompanyInfo::where('uid', Auth::id())->first()) {
            return response(['message' => '请先完善公司信息'], 400);
        }
        $res = $position::create(array_merge($request->all(), ['uid' => Auth::id()]));
        if ($res) {
            return response(['message' => '发布成功']);
        }
        return response(['message' => '发布失败'], 400);
    }

    /**
     * @param ReleasePosition $request
     * @param Position $position
     * 修改职位
     */
    public function updatePosition(ReleasePosition $request, Position $position)
    {
        $res = $position::where('id', $request->id)->update($request->except(['id', 'uid']));
        if ($res) {
            return response(['status' => 1, 'message' => '修改成功', 'data' => $position::where('id', $request->id)->first()]);
        }
        return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
    }

    /**
     * @return mixed
     * 我发布的职位
     */
    public function myReleasePosition(Request $request)
    {
        $pageSize = $request->page_size ? $request->page_size : 15;
        $data = Position::orderBy('id', 'desc')->where('uid', Auth::id())->paginate($pageSize);
        return $data;
    }

    /**
     * @param Request $request
     * @return mixed
     * 查看一条职位信息
     */
    public function lookOnePosition(Request $request)
    {

        $info = Position::where('id', $request->id)->with(['companyInfo'])->first();
        if ($info) {
            $info->look_number = $info->look_number + 1;
            $info->save();
            $me = Auth::id();
            $is_follow = 0;
            if (Follow::where('active_id', $me)->where('position_id', $request->id)->first()) {
                $is_follow = 1;
            }
            $info->is_follow = $is_follow;
            //是否已投递
            $is_send = 0;
            if (ApplicationRecord::where('user_id', $me)->where('position_id', $request->id)->first()) {
                $is_send = 1;
            }
            $info->is_send = $is_send;
        }
        return $info;
    }

    /**
     * @param PositionApply $request
     * @param ApplicationRecord $applicationRecord
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 职位申请
     */
    public function positionApply(PositionApply $request, ApplicationRecord $applicationRecord)
    {
        if (!Basic::where('uid', Auth::id())->first()) {
            return response(['message' => '请先完善个人简历'], 400);
        }
        if ($applicationRecord::where(['user_id' => Auth::id(), 'position_id' => $request->position_id])->first()) {
            return response(['message' => '申请成功', 'is_send' => 1]);
        }
        $is_pinggu = $request->has('is_pinggu') ? $request->get('is_pinggu') : 1;
        $applicationRecord::create([
            'user_id' => Auth::id(),
            'position_id' => $request->position_id,
            'is_pinggu' => $is_pinggu
        ]);
        DB::table('positions')->increment('apply_number');
        return response(['message' => '申请成功', 'is_send' => 1]);
    }

    //是否同意申请
    public function isConsent(IsConsent $request)
    {
        ApplicationRecord::where('id', $request->id)->update($request->only(['status']));
        return response(['message' => '修改成功']);
    }

    /**
     * 职位申请记录
     */
    public function applicationRecord(Request $request)
    {
        $pageSize = $request->page_size ? $request->page_size : 15;
        $sf = UserPersonnel::where('user_id', Auth::id())->value('identity');
        $data = [];
        if ($sf == 1) {//个人
            $res = ApplicationRecord::orderBy('id', 'desc')->where('user_id', Auth::id())->with(['position.companyInfo'])->paginate($pageSize);
            $data['data'] = $res;
            $data['identity'] = 1;
        } elseif ($sf == 2) {//企业
            //根据用户id获得我发布的所有职位id数组
            $position_id_array = Position::orderBy('id', 'desc')->where('uid', Auth::id())->pluck('id');
            $res = ApplicationRecord::orderBy('id', 'desc')->whereIn('position_id', $position_id_array)->with(['userInfo', 'userPersonnel', 'position', 'basics'])->paginate($pageSize);
            $data['data'] = $res;
            $data['identity'] = 2;
        } else {
            $data = [];
        }
        return $data;
    }


    //邀请面试
    public function yaoqing(PositionApply $request)
    {
        if (!\App\CompanyInfo::where('uid', Auth::id())->first()) {
            return response(['message' => '请先完善公司信息'], 400);
        }
        if (Invitationit::where(['user_id' => Auth::id(), 'by_id' => $request->by_id])->first()) {
            return response(['message' => '邀请成功', 'is_send' => 1]);
        }
        Invitationit::create([
            'user_id' => Auth::id(),
            'by_id' => $request->by_id
        ]);
        return response(['message' => '邀请成功', 'is_send' => 1]);
    }

    //是否同意邀请
    public function isInvite(Request $request)
    {
        Invitationit::where('id', $request->id)->update($request->only(['status']));
        return response(['message' => '修改成功']);
    }

    /**
     * 邀请记录
     */
    public function yaoqongjilu(Request $request)
    {
        $pageSize = $request->page_size ? $request->page_size : 15;
        $sf = UserPersonnel::where('user_id', Auth::id())->value('identity');
        $data = [];
        if ($sf == 1) {//个人
            $res = Invitationit::orderBy('id', 'desc')->where('by_id', Auth::id())->with(['company', 'companyInfo'])->paginate($pageSize);
            $data['data'] = $res;
            $data['identity'] = 1;
        } elseif ($sf == 2) {//企业
            $res = Invitationit::orderBy('id', 'desc')->where('user_id', Auth::id())->with(['userInfo', 'userPersonnel', 'basics'])->paginate($pageSize);
            $data['data'] = $res;
            $data['identity'] = 2;
        } else {
            $data = [];
        }
        return $data;
    }

}
